function cascade

load('integral_image.mat');
[image_size , example_number] = size(integral_image);

% Maximum acceptable false positive rate
max_fpr = 0.5;

% Minimum acceptable detection rate
min_dr = 1;

% Target overall false positive rate
F_target = 0.2;

num_of_pos = 400;
num_of_neg = 400;

% N = set of negative examples
rest_of_neg = negatives = integral_image(:, 1:num_of_neg);
% P = set of positive examples
positives = integral_image(:, num_of_neg+101 : num_of_neg+num_of_pos+100);
% Validation set
validation(:, 1:100) = integral_image(:, num_of_neg+1:num_of_neg+100);
validation(:, 101:200) = integral_image(:, num_of_neg+num_of_pos+101:example_numer);

f(1) = 1.0;
d(1) = 1.0;
i = 1;

F_new = F = prod(f);
D_new = D = prod(d);
while F_new > F_target
    i = i + 1;
    N(i) = 0;
    F_new = F;
    while F_new > max_fpr * F
        N(i) = N(i) + 1;
        [threshold(i), parity(i)] = StatBoost(negatives, positives, N[i]);
        [f(i), d(i)] = evaluate(threshold, parity, validation);
        while D_new > min_dr * D
            threshold(i) = threshold(i) - 0.1;
            [f(i), d(i)] = evaluate(threshold, parity, validation);
        end % while    
    end % while
    clear negatives
    num_of_rest = size(rest_of_neg, 2);
    k = 0;
    for j = 1:num_of_rest
        if (try_cascaded_detector(threshold, parity, num_of_rest[j]) == 1)
            negatives[k] = num_of_rest[j];
            k = k + 1;
        end % if
    end % for
    clear num_of_rest
    num_of_rest = negatives;
end % while
